<?php
declare(strict_types=1);
namespace Service\DeliveryService;
use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcModel\Order;
use App\Model\BbcModel\OrderReportStatus;



class ExpressDeliveryService extends BaseService
{
    public function Delivery($company,$param,$response) {
        $companyCode= ucfirst($company);
        $className  = "Service\\DeliveryService\\DeliveryCompany\\{$companyCode}Company";
        $callMethod = 'dealDelivery';
        if (!class_exists($className)) {
            return Normal::returnCode(Normal::FAIL, "服务类【{$className}】不存在");
        }
        if (!method_exists($className, $callMethod)) {
            return Normal::returnCode(Normal::FAIL, "服务类【{$className}】方法【{$callMethod}】不存在");
        }
        return $className::instance()->$callMethod($param,$response);
    }
    
    public function OrderDelivery($expressNo,$status,$receivemobile='') {
        if(empty($expressNo)){
            return Normal::returnCode(Normal::FAIL, "快递单号不能为空");
        }
        if(empty($status)){
            return Normal::returnCode(Normal::FAIL, "状态不能为空");
        }
        if(!in_array($status, [1,2])){
            return Normal::returnCode(Normal::FAIL, "状态参数值不正确");
        }
        $orderWhere=[
            'express_num'=>$expressNo,
            '_order_'    =>['id'=>'desc']
        ];
        if($receivemobile){
            $orderWhere['receive_mobile']=$receivemobile;
        }
        $order= Order::instance()->info($orderWhere);
        if(empty($order)){
            return Normal::returnCode(Normal::FAIL, "订单不存在");
        }
        if($status==1){
            $order_report_status= OrderReportStatus::instance()->info(['order_id'=>$order['id'],'delivery_status'=>1]);
            if(empty($order_report_status)){
                return Normal::returnCode(Normal::FAIL, "单号有误");
            }
            if($order_report_status['delivery_status']==2){
                return Normal::returnCode(Normal::SUC, "该单号已经处理");
            }
            $result= OrderReportStatus::instance()->edit(['id'=>$order_report_status['id']], ['delivery_status'=>2,'delivery_time'=>time(),'md'=>time()]);
            if(!$result){
                return Normal::returnCode(Normal::FAIL, "失败");
            }
        }
        
        if($status==2){
            $order_report_status= OrderReportStatus::instance()->info(['order_id'=>$order['id'],'delivery_status'=>2]);
            if(empty($order_report_status)){
                return Normal::returnCode(Normal::FAIL, "单号有误");
            }
            if($order['status']>=50){
                return Normal::returnCode(Normal::SUC, "该单号已经处理");
            }
            $result=Order::instance()->edit(['id'=>$order['id']],['status'=>50]);
            if(!$result){
                return Normal::returnCode(Normal::FAIL, "失败");
            }
        }
        return Normal::returnCode(Normal::SUC, "成功");
        
    }
    
    public function getinfo($expressNo) {
        if(empty($expressNo)){
            return Normal::returnCode(Normal::FAIL, "快递单号不能为空");
        }
        $orderWhere=[
            'express_num'=>$expressNo,
            '_order_'    =>['id'=>'desc']
        ];
        $order= Order::instance()->info($orderWhere);
        if(empty($order)){
            return Normal::returnCode(Normal::FAIL, "订单不存在");
        }
        return Normal::returnCode(Normal::SUC, "成功",['mobile'=>$order['receive_mobile']]);
    }
}